package com.example.deng.test;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by deng on 2017/4/6.
 */

public class DatabaseHandler1 extends SQLiteOpenHelper{
    private Context mcontext;
    private time t2;
    private DatabaseHandler1 data;
    public static final String DATABASE_NAME="Text";
    public static final String TABLE_NAME="car1";
    public static final String KEY_NUM="num";
    public static final String KEY_TIMEIN="timein";
    public static final String KEY_TIMEOUT="timeout";
    public static final String KEY_TEXTNUM="textnum";
    public static final String CREATE_TABLE="create table "+TABLE_NAME+
            "("+KEY_NUM+" text primary key,"+
                KEY_TIMEIN+" text,"+
                KEY_TIMEOUT+" text,"+
                KEY_TEXTNUM+" integer)";

    private final static int VERSION =1;
    private SQLiteDatabase db;

    public  DatabaseHandler1(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){

        super(context,name,factory,version);
        mcontext=context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
        Toast.makeText(mcontext,"Create Succeded",Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        System.out.println("update Database");
    }


    public void adddata(car c1,DatabaseHandler1 dbHelper) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_NUM, c1.getNumber());
        values.put(KEY_TIMEIN, c1.gettimein());
        values.put(KEY_TEXTNUM, 10);
        db.insert(TABLE_NAME, null, values);
        Cursor cursor = db.query("car1", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            String number = cursor.getString(cursor.getColumnIndex(KEY_NUM));
            String timein = cursor.getString(cursor.getColumnIndex("timein"));
            String timeout = cursor.getString(cursor.getColumnIndex("timeout"));
            Log.d("in", KEY_NUM + number);
            Log.d("in", "time in" + timein);
            Log.d("in", "time out" + timeout);
        }
        db.close();
    }
    public void updateContact(car c1,DatabaseHandler1 dbHelper){
        SQLiteDatabase db= dbHelper.getWritableDatabase();
        Cursor cursor=db.query("car c1",null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            String number = cursor.getString(cursor.getColumnIndex(KEY_NUM));
            String timein = cursor.getString(cursor.getColumnIndex("timein"));
            String timeout = cursor.getString(cursor.getColumnIndex("timeout"));
            Log.d("in", "number" + number);
            Log.d("in", "time in" + timein);
            Log.d("in", "time out" + timeout);
    }


        ContentValues values = new ContentValues();
        values.put(KEY_TIMEOUT, c1.gettimeout());
        values.put(KEY_TIMEIN, 5);
        db.update(TABLE_NAME, values, "KEY_NUM=?", new String[]{c1.getNumber()});
        while(cursor.moveToNext()){
            String number = cursor.getString(cursor.getColumnIndex(KEY_NUM));
            String timein = cursor.getString(cursor.getColumnIndex("timein"));
            String timeout = cursor.getString(cursor.getColumnIndex("timeout"));
            Log.d("in", "number" + number);
            Log.d("in", "time in" + timein);
            Log.d("in", "time out" + timeout);

        }


        db.close();
    }
    public car getCar(String number,DatabaseHandler1 dbHelper) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.query(TABLE_NAME, new String[]{KEY_NUM, KEY_TIMEIN, KEY_TIMEOUT}, KEY_NUM + "=?", new String[]{number}, null, null, null);
        car c1 = null;
        if (cursor.moveToFirst()) {
            c1=new car(cursor.getString(0),cursor.getString(1),cursor.getString(2));
        }
    return c1;
    }

}
















